package cn.jiyun.web.controller;

import cn.jiyun.dao.domain.*;
import cn.jiyun.dao.mapper.MajorMapper;
import cn.jiyun.dao.mapper.TeacherMapper;
import cn.jiyun.service.StuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("stu")
public class StuController {

    @Autowired
    private StuService stuService;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private MajorMapper majorMapper;


    //展示所有
    @RequestMapping("findLimit")
    public String findLimit(@RequestParam(defaultValue = "1")Integer pageNum,
                             Model model, HttpSession session, StuVo stuVo){
       List<Teacher> teacherList = teacherMapper.selectAll();
       List<Major> majorList = majorMapper.selectAll();
       session.setAttribute("teacherList",teacherList);
       session.setAttribute("majorList",majorList);
       PageInfo<StuVo> pageInfo = stuService.findLimit(pageNum,stuVo);
       model.addAttribute("pageInfo",pageInfo);
       model.addAttribute("stuVo",stuVo);
       return "showstu";
    }

    //跳添加界面
    @RequestMapping("findAdd")
    public String findAdd(){
        return "addstu";
    }

    //添加
    @RequestMapping("saveStu")
    public String saveStu( Stu stu ){
        stuService.saveStu(stu);
        return "redirect:findLimit";
    }

    //修改
    @RequestMapping("findUpdate")
    public String findUpdate(Integer id,Model model){
       Stu stu = stuService.findUpdate(id);
       model.addAttribute("stu",stu);
       return "updatestu";
    }

    //逻辑删除
    @RequestMapping("deleteStu")
    public String deleteStu(Integer[] id){
        stuService.deleteStu(id);
        return "redirect:findLimit";

    }

    //详情
    @RequestMapping("findShow")
    public String findShow(Integer id,Model model){
       StuVo vo = stuService.findShow(id);
       model.addAttribute("vo",vo);
       return "xq";
    }

    //跳统计界面
    @RequestMapping("findCount")
    public String findCount(){
        return "zhu";
    }

    //统计
    @RequestMapping("tongji")
    @ResponseBody
    public Map<String,Object> tongji(){
       List<StuCount> list = stuService.tongji();
        HashMap<String, Object> map = new HashMap<>();
        map.put("success",true);
        map.put("list",list);
        return map;
    }

    //状态更改
    @RequestMapping("updateStatus")
    public String updateStatus(Stu stu){
        stuService.updateStatus(stu);
        return "redirect:findLimit";
    }

    //导出
    @RequestMapping("outStu")
    public void outStu( HttpServletResponse response ) throws IOException {
        stuService.outStu(response);
    }



}
